//导入数据库模块
const db = require('../db/index.js')


//添加
exports.addCar = (req, res) => {
    console.log(req.body)
    const carInfo = req.body
    const sqlStr = 'INSERT INTO cars (name,  url, description, date) VALUES (?, ?, ?, ?)'
    db.query(sqlStr, [carInfo.name,  carInfo.url, carInfo.description, carInfo.date], (err, results) => {
        if (err) {
            return console.log(err)
        }
        console.log(results)
        if (results.affectedRows === 1) {
            const id = results.insertId
            for (let i = 0; i < carInfo.category.length; i++){
                const sqlStr = 'INSERT INTO proCate (product, category) VALUES (?, ?) '
                db.query(sqlStr, [id,carInfo.category[i]], (err, results) => {
                    if (err) {
                        return console.log(err)
                    }
                    if (results.affectedRows === 1) {
                        console.log('插入分类成功')
                    }
                })
            }
            console.log('插入数据成功')
            res.send({
                status: 0,
                message: '添加成功'
            })
        }
    })
}

//获取首页展示车子的图片
exports.getshowCarlist = (req, res) => {
    //请求的分类

    const content = req.query.isshow
        console.log(content)
        const sqlStr = 'SELECT * FROM cars JOIN proCate ON cars.id = proCate.product WHERE cars.isshow= ?;'
    // const sqlStr = "SELECT * FROM cars WHERE isshow=? "
    db.query(sqlStr, [content], (err, results) => {
        if (err) {
            console.log(err)
            return res.send('err')
        }
        // console.log(results.length)
        // res.send(results)
                // 去重处理
        const unique = Array.from(new Map(results.map(item => [item['id'], item])).values());
        console.log(unique.length)
        res.send(unique);
    })
}
    
    


//获取产品列表
exports.getCarlist = (req, res) => {
    //请求的分类
    const id = req.query.id


    console.log(req.query.id, "请求参数")
    //如果判断分类
    if (req.query.id || req.query.id === 0) {
        console.log(req.query.id)
        // const sqlStr = 'SELECT * FROM cars WHERE category=?'
        const sqlStr = 'SELECT * FROM cars JOIN proCate ON cars.id = proCate.product WHERE proCate.category = ?;'
        db.query(sqlStr, [id], (err, results) => {
            if (err) {
                console.log(err,"连接数据库报错")
                return res.send(err)
            }
            console.log(results)
            res.send(results)
        })
    } else {

// SELECT
//     products.product_id,
//     products.product_name,
//     categories.category_id,
//     categories.category_name
// FROM
//     products
// JOIN
//     product_category ON products.product_id = product_category.product_id
// JOIN
//     categories ON product_category.category_id = categories.category_id;



        const sqlStr = 'SELECT cars.id AS id, cars.name AS name, GROUP_CONCAT(proCate.category) AS category , cars.url AS url, cars.description AS description FROM cars JOIN proCate ON cars.id = proCate.product GROUP BY cars.id, cars.name, cars.url, cars.description ;'
        db.query(sqlStr, (err, results) => {
            if (err) {
                console.log(err)
                return res.send('err')
            }
            console.log(results)
            res.send(results)
        })
    }
    
    
}

//通过产品id查询产品
exports.getIdCarlist = (req, res) => {
    //请求的id
    const id = req.query.id

    console.log(req.query.id, "请求的id")
        console.log(req.query.id)
        // const sqlStr = 'SELECT * FROM cars WHERE category=?'
        const sqlStr = 'SELECT * FROM cars  WHERE id = ?;'
        db.query(sqlStr, [id], (err, results) => {
            if (err) {
                console.log(err,"连接数据库报错")
                return res.send(err)
            }
            console.log(results)
            res.send(results)
        })

    
}

// exports.getCarlist = (req, res) => {

//     const id = parseInt(req.query.id)
//     // const sqlStr = 'SELECT * FROM cars JOIN pro-cate ON cars.id = pro-cate.product WHERE id=?'
//     const sqlStr = 'SELECT * FROM cars JOIN proCate ON cars.id = proCate.product WHERE proCate.category = ?;'
//     db.query(sqlStr, [id], (err, results) => {
//         if (err) {
//             console.log(err)
//             return res.send(err)
//         }
//         console.log(results)
//         res.send(results)
//     })
    
// }

//
exports.deleteCar = (req, res) => {
    console.log(req.query.id, '请的id')
    const sqlStr = 'DELETE FROM cars WHERE id=?'
    db.query(sqlStr, req.query.id, (err, results) => {
        if (err) {
            console.log(err)
        }
        if (results.affectedRows === 1) {
            res.send({
                status: 0,
                message: '删除成功'
            })
        }
    })
}